package com.k8sops.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.k8sops.backend.mapper.DOSReleaseMapper;
import com.k8sops.backend.model.entity.DOSRelease;
import com.k8sops.backend.model.vo.ReleaseVO;
import com.k8sops.backend.service.DOSReleaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * DOSRelease服务实现类
 */
@Service
@Slf4j
public class DOSReleaseServiceImpl extends ServiceImpl<DOSReleaseMapper, DOSRelease> implements DOSReleaseService {

    @Override
    public boolean saveDeployment(DOSRelease dosRelease) {
        log.info("保存部署信息: {}", dosRelease);
        return this.save(dosRelease);
    }
    
    @Override
    public IPage<DOSRelease> pageReleases(Page<DOSRelease> page, Integer projectId, Integer moduleId, 
                                         Integer envType, Integer clusterId, Integer namespaceId) {
        LambdaQueryWrapper<DOSRelease> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据条件过滤
        if (projectId != null) {
            queryWrapper.eq(DOSRelease::getProjectId, projectId);
        }
        if (moduleId != null) {
            queryWrapper.eq(DOSRelease::getModuleId, moduleId);
        }
        if (envType != null) {
            queryWrapper.eq(DOSRelease::getEnvType, envType);
        }
        if (clusterId != null) {
            queryWrapper.eq(DOSRelease::getClusterId, clusterId);
        }
        if (namespaceId != null) {
            queryWrapper.eq(DOSRelease::getNamespaceId, namespaceId);
        }
        
        // 按创建时间降序排序，显示最新的部署
        queryWrapper.orderByDesc(DOSRelease::getCreatedAt);
        
        return page(page, queryWrapper);
    }
    
    @Override
    public IPage<DOSRelease> pageReleasesWithProjectInfo(Page<DOSRelease> page, Integer projectId, Integer moduleId,
                                                      Integer envType, Integer clusterId, String namespace, String releaseName, Integer projectGroupId) {
        log.info("查询Release列表，并关联项目和项目组信息: projectId={}, moduleId={}, envType={}, clusterId={}, namespace={}, releaseName={}, projectGroupId={}",
                projectId, moduleId, envType, clusterId, namespace, releaseName, projectGroupId);
        
        // 构建查询条件
        LambdaQueryWrapper<DOSRelease> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据条件过滤
        if (projectId != null) {
            queryWrapper.eq(DOSRelease::getProjectId, projectId);
        }
        if (moduleId != null) {
            queryWrapper.eq(DOSRelease::getModuleId, moduleId);
        }
        if (envType != null) {
            queryWrapper.eq(DOSRelease::getEnvType, envType);
        }
        if (clusterId != null) {
            queryWrapper.eq(DOSRelease::getClusterId, clusterId);
        }
        if (StringUtils.isNotBlank(namespace)) {
            queryWrapper.like(DOSRelease::getNamespaceId, namespace);
        }
        if (StringUtils.isNotBlank(releaseName)) {
            queryWrapper.like(DOSRelease::getReleaseName, releaseName);
        }
        
        // 按创建时间降序排序，显示最新的部署
        queryWrapper.orderByDesc(DOSRelease::getCreatedAt);
        
        // 使用Mapper中的关联查询方法
        Page<ReleaseVO> voPage = new Page<>(page.getCurrent(), page.getSize());
        IPage<ReleaseVO> releaseVOPage = baseMapper.selectReleasesWithProjectInfo(voPage, queryWrapper);
        
        // 将ReleaseVO转换为DOSRelease返回，以符合接口定义
        Page<DOSRelease> resultPage = new Page<>(releaseVOPage.getCurrent(), releaseVOPage.getSize(), releaseVOPage.getTotal());
        // 这里需要进行类型转换，因为ReleaseVO是DOSRelease的子类，可以安全地进行转换
        resultPage.setRecords((java.util.List<DOSRelease>)(java.util.List<?>)releaseVOPage.getRecords());
        
        return resultPage;
    }
    
    @Override
    public boolean existsByReleaseName(String releaseName) {
        if (StringUtils.isBlank(releaseName)) {
            return false;
        }
        
        LambdaQueryWrapper<DOSRelease> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DOSRelease::getReleaseName, releaseName);
        
        // 只需要计数，不需要查询具体数据
        return this.count(queryWrapper) > 0;
    }
}
